package cn.btks.backstagemanage.menu.controller;

import cn.btks.backstagemanage.menu.dto.MenuDto;
import cn.btks.backstagemanage.menu.pojo.Menu;
import cn.btks.backstagemanage.menu.pojo.MenuRolesDto;
import cn.btks.backstagemanage.menu.service.MenuService;
import cn.btks.backstagemanage.user.pojo.User;
import cn.btks.commonserver.result.MenuSourceEnum;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.commonserver.result.SystemLogEnum;
import cn.btks.commonserver.valid.SystemLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Author lijun
 * @Date 2021/1/11 19:06
 */
@RestController
@RequestMapping("api/v1.0/menu")
@Api(tags = "资源清单模块")
@Validated
public class MenuController {

    @Autowired
    MenuService menuService;

    @GetMapping("/findAll")
    @ApiOperation("查询所有资源清单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAll() {
        List<Menu> menuList = menuService.findAll();
        return menuList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), menuList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findAllMenuDtos")
    @ApiOperation("查询所有资源清单树")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllMenuDtos() {
        List<MenuDto> menuDtoList = menuService.findAllMenuDto();
        return menuDtoList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), menuDtoList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findById/{id}")
    @ApiOperation("根据ID查询资源清单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "资源ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findById(@PathVariable("id")Long id) {
        Menu menu = menuService.findById(id);
        return menu != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), menu) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findAllByRoleId/{roleId}")
    @ApiOperation("根据角色ID查询资源清单树信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "roleId", value = "角色ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllByRoleId(@PathVariable("roleId")Long roleId) throws IOException {
        //改为加密
        List<MenuDto> menuDtoList = menuService.findByRoleId(roleId);
        return menuDtoList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), menuDtoList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findAllByUserId/{userId}")
    @ApiOperation("根据用户ID查询其角色下关联的资源清单树信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "userId", value = "用户ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllByUserId(@PathVariable("userId")Long userId) throws IOException {
        //改为加密
        List<MenuDto> menuDtoList = menuService.findAllByUserId(userId);
        return menuDtoList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), menuDtoList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @GetMapping("/findPages")
    @ApiOperation("分页查询资源清单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "资源名称", required = false, dataType = "string"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState findPagesByName(Integer pageNumber, Integer pageSize, String name) {
        return menuService.findPageList(pageNumber, pageSize, name);
    }

    @SystemLog(type = SystemLogEnum.SAVE_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @PostMapping("/save")
    @ApiOperation(value = "保存资源清单信息", notes = "保存资源清单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "url", value = "路径匹配规则", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "path", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "component", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "iconCls", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "keepAlive", value = "", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "requireAuth", value = "", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "parentId", value = "", required = false, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "menuOrder", value = "排序", required = false, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "unitElement", value = "单元元素", required = false, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "menuLevel", value = "菜单等级（1：一级菜单 2：二级菜单 3：三级菜单）", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "isResource", value = "是否资源（0：菜单 1：资源 2：按钮）", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "menuName", value = "菜单名称（资源则为空）", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "roles", value = "角色对象列表", required = true, dataType = "list"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState save(@RequestBody Menu menu){
        return menuService.save(menu);
    }

    @SystemLog(type = SystemLogEnum.SAVE_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @PostMapping("/saveAll")
    @ApiOperation(value = "批量保存资源清单信息", notes = "批量保存资源清单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "url", value = "路径匹配规则", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "path", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "component", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "iconCls", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "keepAlive", value = "", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "requireAuth", value = "", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "parentId", value = "", required = false, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "menuOrder", value = "排序", required = false, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "unitElement", value = "单元元素", required = false, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "menuLevel", value = "菜单等级（1：一级菜单 2：二级菜单 3：三级菜单）", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "isResource", value = "是否资源（0：菜单 1：资源 2：按钮）", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "menuName", value = "菜单名称（资源则为空）", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "roles", value = "角色对象列表", required = true, dataType = "list"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState saveAll(@RequestBody List<Menu> menus){
        return menuService.saveAll(menus);
    }


    @SystemLog(type = SystemLogEnum.UPDATE_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @PostMapping("/update")
    @ApiOperation(value = "更新资源清单信息", notes = "更新资源清单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "资源清单ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "url", value = "路径匹配规则", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "path", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "component", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "iconCls", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "keepAlive", value = "", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "requireAuth", value = "", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "parentId", value = "", required = false, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "menuOrder", value = "排序", required = false, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "unitElement", value = "单元元素", required = false, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "menuLevel", value = "菜单等级（1：一级菜单 2：二级菜单 3：三级菜单）", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "isResource", value = "是否资源（0：菜单 1：资源 2：按钮）", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "menuName", value = "菜单名称（资源则为空）", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "roles", value = "角色对象列表", required = true, dataType = "list"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState update(@RequestBody Menu menu){
        return menuService.update(menu);
    }

    @SystemLog(type = SystemLogEnum.UPDATE_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @PostMapping("/updateMenuRoles")
    @ApiOperation(value = "根据角色ID更新关联资源清单信息", notes = "根据角色ID更新关联资源清单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "roleId", value = "角色ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "menuIdList", value = "角色关联资源清单ID列表", required = true, dataType = "list"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState update(@RequestBody MenuRolesDto menuRolesDto) throws IOException {
        //改为加密
        return menuService.updateMenuRoles(menuRolesDto);
    }

    @SystemLog(type = SystemLogEnum.DELETE_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除资源清单信息", notes = "删除资源清单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "资源清单ID", required = true, dataType = "long"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState delete(@PathVariable("id") Long id) throws IOException {
        //改为加密
        return menuService.delete(id);
    }

    @SystemLog(type = SystemLogEnum.DELETE_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @DeleteMapping("/deleteConnection/{id}")
    @ApiOperation(value = "删除资源清单信息关联角色信息", notes = "删除资源清单信息关联角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "资源清单ID", required = true, dataType = "long"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState deleteConnection(@PathVariable("id") Long id) throws IOException {
        //改为加密
        return menuService.deleteConnection(id);
    }

    @GetMapping("/test")
    @ApiOperation("测试")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState test() throws IOException {
        return menuService.test();
    }
}
